package com.google.gwt.sample.dynatablerf.client.home.views;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.sample.dynatablerf.client.home.presenter.ProdutoCrudPresenter.Display;
import com.google.gwt.sample.dynatablerf.server.business.ControladorFactory;
import com.google.gwt.sample.dynatablerf.server.domain.Produto;
import com.google.gwt.sample.dynatablerf.shared.ProdutoProxy;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ProdutoCrudView extends Composite implements Display{

	private VerticalPanel mainPanel;
	private Label valorLabel;

	private Label descricaoLabel;
	private TextArea descricaoArea;

	private Label nomeLabel;
	private TextBox nomeBox;

	private  NumberFormat activeFormat;
	private  Label formattedBox;
	private  TextBox valueBox ;
	
	private  Button uploadButton;

	public ProdutoCrudView(){
		mainPanel = new VerticalPanel();


		init();
		initWidget(mainPanel);
	}

	private void init() {

		activeFormat = NumberFormat.getCurrencyFormat();

		HorizontalPanel painelNome = new HorizontalPanel();

		nomeLabel = new Label("Nome Produto:");
		nomeBox = new TextBox();

		painelNome.add(nomeLabel);
		painelNome.add(nomeBox);


		valorLabel = new Label("Valor Produto:");
		valorLabel.setWidth("17em");
		

		valueBox = new TextBox();
		valueBox.addKeyUpHandler(new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				updateFormattedValue();
			}
		});

		formattedBox = new Label();
		formattedBox.setWidth("17em");
		
		HorizontalPanel painelValor = new HorizontalPanel();
		
		painelValor.add(valorLabel);
		painelValor.add(valueBox);
		painelValor.add(formattedBox);



		descricaoLabel = new Label("Descição Produto:");
		descricaoArea = new TextArea();
		descricaoArea.setWidth("400px");
		descricaoArea.setHeight("180px");
		
		mainPanel.add(painelNome);
		mainPanel.add(painelValor);
		mainPanel.add(descricaoLabel);
		mainPanel.add(descricaoArea);
		
		mainPanel.add(fileUpload());
		
		
		
		
	}


	private void updateFormattedValue() {
		String sValue = valueBox.getText();
		if (!sValue.equals("")) {
			try {
				double value = Double.parseDouble(sValue);
				String formattedValue = activeFormat.format(value);
				formattedBox.setText(formattedValue);
				showErrorMessage(null);
			} catch (NumberFormatException e) {
				showErrorMessage("Por favor apenas numeros");
			}
		} else {
			formattedBox.setText("");
		}
	}

	private void showErrorMessage(String errorMsg) {
		if (errorMsg == null) {
			formattedBox.removeStyleName("cw-RedText");
		} else {
			formattedBox.setText(errorMsg);
			formattedBox.addStyleName("cw-RedText");
		}
	}
	
	public Widget fileUpload(){
		
		 // Create a vertical panel to align the content
	    VerticalPanel vPanel = new VerticalPanel();

	    // Add a label
	    vPanel.add(new HTML("Selecione um arquivo"));

	    // Add a file upload widget
	    final FileUpload fileUpload = new FileUpload();
	    fileUpload.ensureDebugId("cwFileUpload");
	    vPanel.add(fileUpload);

	    // Add a button to upload the file
	    uploadButton = new Button("Salvar");
	    vPanel.add(new HTML("<br>"));
	    vPanel.add(uploadButton);

	    // Return the layout panel
	    return vPanel;
	}

	@Override
	public void copyTo(ProdutoProxy proxy) {
		proxy.setDescricao(descricaoArea.getValue());
		proxy.setNome(nomeBox.getValue());
		proxy.setValor(Double.parseDouble(valueBox.getValue()));
		
	}
	
	@Override
	public HasClickHandlers butaoSalvar(){
	
		return uploadButton;
	}


}
